/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package json;

import engine.Player;
import engine.Singleton;
import engine.Soluter;
import engine.randomizer;
import java.util.List;
import server.Server;
import org.apache.log4j.Logger;

/**
 *
 * @author User
 */
public class Controller implements IController{
  private Server mysrv;
  private static final Logger log = Logger.getLogger(Protocol.class);
  
  public Controller(Server mysrv)
  {
    this.mysrv = mysrv;
  }
  
  @Override
  public void execute(String command, String name, String offerName, String row, String col)
  {
    if (command.equals("Autorization")){
      preautorization(name);
      mysrv.getPlayer(mysrv.getPos()).setLogin(name);
      autorization(name);
    }
    if (command.equals("Partner")){
       sendOffer(name, offerName);
    }
    if (command.equals("Refuse")){
       refuse(name);
    }
    if (command.equals("StartGame")){
       startGame(name, offerName);
    }
    if (command.equals("Step")){
       int pos = findGame(name);
       Singleton.sol2.get(pos).step(row, col);
    }
    if (command.equals("End")){
      String send = "{\"Type\":\"End\"}";
      mysrv.sendUser(send, -1);
      delUser(mysrv.getPlayer(mysrv.getPos()).getLogin());
    }
    if (command.equals("Wait")){
       String send = "{\"Type\":\"Wait\"}";
       mysrv.sendUser(send, -1);
    }
    if (command.equals("Refresh")){
       List<Player> users = Singleton.clients;
         String out = "{\"Type\":\"Users\", \"Name\":\"";
         for (int i = 0; i < users.size(); i++) {
           if(users.get(i).state){
             out += users.get(i).getLogin();
             if (i < (users.size() - 1)) {
              out += "-";
           }
           }
         }
         out += "\"}";
         mysrv.sendUser(out, -1);
    }
    
    
  }
  
  private int findGame(String name){
     int count = 0;
     int pos = -1;
     for (Soluter p : Singleton.sol2){
        if(p.play1.getLogin().equals(name) || p.play2.getLogin().equals(name)){
           pos = count;
        }
        count++;
     } 
     return pos;
  }
  
  private void delUser(String user)
  {
    int pos = SerachPlayer(user);
    Singleton.clients.remove(pos);
  }
  
  private void preautorization (String login)
  {
    int pos = -1;
    int count =0;
    for (Player s : Singleton.clients){
      if (s.getLogin() != null){
        if (s.getLogin().equals(login)) {
          pos = count;
        }
      }
      
      count++;
    }
    if (pos != -1){
      Singleton.clients.remove(pos);
    }
  }
  
  public void startGame(String player1, String player2)
  {
    String fig1, fig2, step1, step2;
    log.debug("startGame Player1 " + player1 + " " + player2);
    
    int pos1 = SerachPlayer(player1);
    Player play1 = Singleton.clients.get(pos1);
    int pos2 = SerachPlayer(player2);
    Player play2 = Singleton.clients.get(pos2);
    log.debug("Send first inf Player1 " + pos1 + " " + play1.getLogin() + " player2 " + pos2 + " " + play2.getLogin());
    randomizer rand = new randomizer();
    boolean figure = rand.firstStep();
    boolean step = rand.firstStep();
    if (figure){
      fig1 = "Cross";
      fig2 = "Circle";
    }
    else {
      fig1 = "Circle";
      fig2 = "Cross";
    }
    
    if (step){
      step1 = "First";
      step2 = "Second";
    }
    else {
      step1 = "Second";
      step2 = "First";
    }
      
    String send1 = "{\"Type\":\"StartGame\", \"Figure\":\"" + fig1 + "\", \"Step\":\"" + step1 + "\"}";
    String send2 = "{\"Type\":\"StartGame\", \"Figure\":\"" + fig2 + "\", \"Step\":\"" + step2 + "\"}";
    play1.getSocket().sendUser(send1, pos1);
    play2.getSocket().sendUser(send2, pos2);
    play1.state = false;
    play2.state = false;
    //Singleton.sol = new Soluter(play1, fig1, step1, play2, fig2, step2);
    Singleton.sol2.add(new Soluter(play1, fig1, step1, play2, fig2, step2));
  }
  
  private int SerachPlayer(String login)
  {
    int pos = 0;
    int count =0;
    for (Player s : Singleton.clients){
      if (s.getLogin().equals(login)){
        pos = count;
      }
      count++;
    }
    return pos;
  }
  
  private void refuse(String login)
  {
    int pos = SerachPlayer(login);
    Player t = Singleton.clients.get(pos);
    String send = "{\"Type\":\"NoGame\"}";
    t.getSocket().sendUser(send, pos);
  }
  
  private void sendOffer(String login, String offerName)
  {
    int pos = SerachPlayer(login);
    //List<ServeOneThread> clients = Singleton.clients;
    Player t = Singleton.clients.get(pos);
    String send = "{\"Type\":\"Offer\", \"Name\":\"" + offerName + "\"}";
    t.getSocket().sendUser(send, pos);
  }
  
  private void autorization(String name){
    Singleton.getInstance().setUser(name);
    List<Player> users = Singleton.clients;
   // Singleton.out = "{\"Type\":\"Users\", \"Data\":{"; 
    String out = "{\"Type\":\"Users\", \"Name\":\""; 
    for(int i=0; i<users.size(); i++)
    {
     // Singleton.out +="\"Name\":\"" + users.get(i) + "\"";
      if (users.get(i).state) {
        out += users.get(i).getLogin();
        if (i < (users.size()-1))
        out += "-";
      }
       
    }
   // Singleton.out += "}}";
    out += "\"}";
  //  Singleton.out += "\n{\"Type\": \"End\"}";
    mysrv.sendUser(out, -1);
  }
  
}
